package com.wsl.myexception.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@Slf4j
public class HttpAspect {
    /**
     * 切点
     */
    @Pointcut("execution(public * com.wsl.myexception.controller.AspectController.get(..))")
    public void log(){
    }

    /**
     * 方法执行前
     * @param joinPoint
     */
    @Before("log()")
    public void beforelog(JoinPoint joinPoint){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //url
       log.info("url={}",request.getRequestURL());
        //method
        log.info("method={}",request.getMethod());
        //ip
        log.info("ip={}",request.getRemoteAddr());
        //类方法
        log.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        //参数
        log.info("args={}",joinPoint.getArgs());
    }

    /**
     * 方法执行后
     */
    @After("log()")
    public void afterLog(){
        log.info("我是after");
    }

    /**
     * 打印方法的返回参数
     * @param object
     */
    @AfterReturning(returning = "object",pointcut = "log()")
    public void returnRest(Object object){
        log.info("response{}",object);
    }
}
